package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.LklMerchantSet;
import com.xmy.cultivate.entity.LklMerchantTeacher;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.TeacherPoint;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2025-03-21
 */
public interface LklMerchantTeacherMapper extends BaseMapper<LklMerchantTeacher> {

/*
    @Select({ "<script>",
            "SELECT",
            "s.`name` AS teacherName,",
            "sj.`name` AS subjectsName,",
            "t.*",
            "FROM",
            "lkl_merchant_teacher t",
            "LEFT JOIN staff s ON t.teacher_id = s.id",
            " LEFT JOIN subjects sj ON t.subjects_id = sj.id",
            "where",
            "1=1",
            "AND t.merchant_id = ${id}",
            "<if test ='name != null and name !=&apos;&apos;'>",
            "AND s.`name` LIKE '%${name}%' ",
            "</if>",
            "<if test ='subjectsId != null and subjectsId !=&apos;&apos;'>",
            "AND t.subjects_id = ${subjectsId} ",
            "</if>",
            "ORDER BY t.created_at DESC",
            "</script>"})

    public IPage<LklMerchantTeacher> getLklMerchantTeacherList(IPage<LklMerchantTeacher> page, @Param("subjectsId") String subjectsId, @Param("name") String name,  @Param("id") String id);
*/


    @Select({ "<script>",
            "SELECT",
            "s.`name` AS teacherName,",
            "sj.`name` AS subjectsName,",
            " g.subjects_id AS subjectsId,",
            "t.id,",
            "t.merchant_id,",
            "t.merchant_set_id,",
            "t.teacher_id,t.lesson_type",
//            "t.*",
            "FROM",
            "lkl_merchant_teacher t",
            "LEFT JOIN staff s ON t.teacher_id = s.id",
            "LEFT JOIN grade g ON t.teacher_id = g.teacher_id",
            "AND g.lesson_type = 1",
            "AND g.year_part = ${yearPart}",
            "AND g.quarter_num = ${quarterNum}",
            "LEFT JOIN subjects sj ON g.subjects_id = sj.id ",
            "where",
            "1=1",
//            "<if test ='yearPart != null and yearPart !=&apos;&apos;'>",
//            "AND g.year_part = ${yearPart}",
//            "</if>",
//            "<if test ='quarterNum != null and quarterNum !=&apos;&apos;'>",
//            "AND g.quarter_num = ${quarterNum}",
//            "</if>",

            "AND t.lesson_type = 1 ",
            "AND t.merchant_id = ${id}",
            "<if test ='name != null and name !=&apos;&apos;'>",
            "AND s.`name` LIKE '%${name}%' ",
            "</if>",
            "<if test ='subjectsId != null and subjectsId !=&apos;&apos;'>",
            "AND g.subjects_id = ${subjectsId} ",
            "</if>",
            "GROUP BY t.teacher_id",
            "ORDER BY t.created_at DESC",
            "</script>"})

    public IPage<LklMerchantTeacher> getLklMerchantTeacherList(IPage<LklMerchantTeacher> page, @Param("subjectsId") String subjectsId, @Param("name") String name,  @Param("id") String id, @Param("yearPart") String yearPart, @Param("quarterNum") String quarterNum);



    @Select({ "<script>",
            "SELECT",
            "s.id AS student_id,",
            "s.name,",
            "s.sex,",
            "s.head_img,",
            "s.linkman_phone,",
            "sg.reading_status",
            "FROM",
            "grade g",
            "JOIN student_grade sg ON g.id = sg.grade_id",
            "AND g.year_part = sg.year_part",
            "AND g.quarter_num = sg.quarter_num",
            "JOIN student s ON sg.student_id = s.id",
            "WHERE",
            "g.teacher_id = ${teacherId}",
            "AND sg.is_last = 1",
            "AND g.lesson_type = 1",
            "AND g.deleted = 0",
            "AND g.year_part = ${yearPart}",
            "AND g.quarter_num = ${quarterNum}",
            "<if test='name != null and name != &apos;&apos;'>",
            "AND s.name LIKE CONCAT('%', #{name}, '%')",
            "</if>",
            "</script>"})
    public IPage<LklMerchantSet> getLklMerchantTeacherListDetails(IPage<LklMerchantSet> page, @Param("yearPart") String yearPart, @Param("quarterNum") String quarterNum, @Param("teacherId") Long teacherId, @Param("name") String name);


    @Select({ "<script>",
            "SELECT",
            "t1.merchant_name",
            "FROM",
            "lkl_merchant_set t1",
            "JOIN lkl_merchant t2 ON t1.id = t2.merchant_set_id ",
            "WHERE",
            "t2.id = ${merchatId}",
            "LIMIT 1",
            "</script>"})
    public String getMerchantName(@Param("merchatId") Long merchatId);



    @Select({ "<script>",
            "SELECT",
//            "t2.*,",
            "getSchoolName(t2.school_id) AS schoolName,",
            "t3.merchant_name",
            "FROM",
            "lkl_merchant_teacher t1",
            "LEFT JOIN lkl_merchant t2 ON t1.merchant_id = t2.id",
            "LEFT JOIN lkl_merchant_set t3 ON t1.merchant_set_id = t3.id",
            "WHERE",
            "t1.teacher_id = #{teacherId}",
            "LIMIT 1",
            "</script>" })
    public LklMerchantTeacher existLklMerchantTeacher(@Param("teacherId") Long teacherId);


}
